<?php
/*
 * 地区分类
 */
namespace Admin\Model;
use Think\Model;

/**
 * Area模型
 */
class areaModel extends Model{
    
    protected $tableName = 'area';


    /*
     * 获取信息
     */
    public function getArea($where){
        $res = $this->where($where)->find();
        return $res;
    }
    
    /*
     * 获取信息
     */
    public function getAreaList(){
        $res = $this->order('gtype_ord ASC')->select();
        return $res;
    }
    
    /*
     * 根据地区编码, 获取省市县
     */
    public function getAreaAddress($area_id){
        $where = array("area_id"=>$area_id);
        $res = $this->where($where)->find();
        if(!$res){
            return false;
        }
        $level = $res['level'];
        
        $data = array();
        if($level == 3){
            $city_id = $res['parentid'];
            $city = $this->where(array("area_id"=>$city_id))->find();
            $province_id = $city['parentid'];
            $province = $this->where(array("area_id"=>$province_id))->find();
            
            $data = array(
                "province" => $province,
                "city" => $city,
                "country" => $res
            );
        }else if($level == 2){
            $province_id = $res['parentid'];
            $province = $this->where(array("area_id"=>$province_id))->find();
            
            $data = array(
                "province" => $province,
                "city" => $res,
            );
        }else if($level == 1){
            $data = array(
                "province" => $res,
            );
        }
        
        return $data;
        
    }

}
